/**
 * 处理表单数据
 */

export default {
    methods: {
        // 处理日期选择改变 v:value key:日期字段名
        handleDateChange (v, key, cb) {
            this.form[key] = v
            cb && cb()
        },
        // 许可证格式化: 字母+数字，两位-两位-14位（输入前四位自动加上-）
        fmtLicenseNo () {
            const value = this.form.licenseNo
            const v = (value || '').replace(/-/g, '')
            if (v) {
                if (/^[0-9a-zA-Z]+$/.test(v)) {
                    let v1 = v.substr(0, 2)
                    const v2 = v.substr(2, 2)
                    const v3 = v.substr(4, 14)
                    v1 += v2 ? `-${v2}` : ''
                    v1 += v3 ? `-${v3}` : ''
                    this.form.licenseNo = v1
                }
            }
        },

        /**
         * 处理 境内收/发货人 改变
         */
        handleTradeIdChange (data, key) {
            let keys = ['tradeCode', 'tradeCoScc', 'tradeName', 'tradeCiqCode']
            const flag = keys.every(k => {
                return !this.form[k] || k === key
            })
            // 增加海关代码可忽略其他选项是否有值并带出其他选项的值(TAPD:1007750)
            let isCustomCode = flag ? false : key === 'tradeCode'
            if (isCustomCode || flag && data) {
                const { operatorName, coScc, operatorCode, operatorId, operatorCiq } = data
                const values = [operatorCode, coScc, operatorName, operatorCiq]
                // 海关代码带出名称和18位信用代码 检验检疫代码不需要带出(TAPD:1007750)
                isCustomCode && keys.pop()
                keys.forEach((k, index) => {
                    if (k !== key) {
                        this.form[k] = values[index]
                        // 添加 境内收/发货人 校验(TAPD_BugID: 1020896,1021496,1021704)
                        this.validateField(k)
                    }
                })
                this.form.tradeId = operatorId
            }
        },

        // 境外收货人 改变事件
        handleOverseasChange (data, key) {
            const keys = ['overseasConsignorCode', 'overseasConsignorEname']
            const flag = keys.every(k => {
                return k === key || !this.form[k]
            })
            if (flag && data) {
                const { overseasCode, overseasName } = data
                const values = [overseasCode, overseasName]
                keys.forEach((k, index) => {
                    if (k !== key) {
                        this.form[k] = values[index]
                        this.validateField(k)
                    }
                })
            }
        },
        /**
         * 处理消费使用/生产销售单位改变
         */
        handleOwnerIdChange (data, key) {
            let keys = ['ownerCode', 'ownerCodeScc', 'ownerName', 'ownerCiqCode']
            const flag = keys.every(k => {
                return !this.form[k] || k === key
            })
            // 增加海关代码可忽略其他选项是否有值并带出其他选项的值(TAPD:1007750)
            let isCustomCode = flag ? false : key === 'ownerCode'
            if (isCustomCode || flag && data) {
                const { operatorName, coScc, operatorCode, operatorId, operatorCiq } = data
                const values = [operatorCode, coScc, operatorName, operatorCiq]
                // 海关代码带出名称和18位信用代码 检验检疫代码不需要带出(TAPD:1007750)
                isCustomCode && keys.pop()
                keys.forEach((k, index) => {
                    if (k !== key) {
                        this.form[k] = values[index]
                        // 添加  消费使用/生产销售 校验(TAPD_BugID: 1020896,1021496,1021704)
                        this.validateField(k)
                    }
                })
                this.form.ownerId = operatorId
            }
        }
    }
}
